package com.laiyima.zhongjiang.linghuilv.demo.Serviceprovider;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.laiyima.zhongjiang.linghuilv.demo.R;
import com.laiyima.zhongjiang.linghuilv.demo.adapter.MachineAdapter;
import com.laiyima.zhongjiang.linghuilv.demo.bean.MachineBean;
import com.laiyima.zhongjiang.linghuilv.demo.util.TimeUtils;
import com.laiyima.zhongjiang.linghuilv.demo.view.ListViewPlus;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
  * create an instance of this fragment.
 */
public class UnactivationFragment extends Fragment implements ListViewPlus.ListViewPlusListener{
    private LinearLayout nu_bindingList;
    private ListViewPlus mListView;
    private int bCode;
    private int count, page_count, page_index;
    private MachineBean mSnListBean;
    private MachineAdapter mSnlistAdapter;
    private List<MachineBean> SnListBeanListBean = new ArrayList<>();

    @Override
    public void onRefresh() {
        SnListBeanListBean.clear();
        intView();
        mSnlistAdapter.notifyDataSetChanged();

        loadData(ListViewPlus.REFRESH);
        // Log.e(TAG, "onRefresh:");

    }

    @Override
    public void onLoadMore() {
        if (page_index < page_count) {
            loadData(ListViewPlus.LOAD);
        } else {
            Toast.makeText(getContext().getApplicationContext(), "已全部加载", Toast.LENGTH_SHORT).show();
            onLoadComplete();
        }
    }

    private void loadData(final int what) {
        // 这里模拟从服务器获取数据
        new Thread(new Runnable() {
            @Override
            public void run() {

                Message msg = handler.obtainMessage();
                msg.what = what;
                handler.sendMessage(msg);
            }
        }).start();
    }

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case ListViewPlus.REFRESH:
                    onLoadComplete();
                    Toast.makeText(getContext().getApplicationContext(), "已刷新数据", Toast.LENGTH_SHORT).show();
                    break;
                case ListViewPlus.LOAD:
                    page_index++;
                    loadMode(page_index);
                    Toast.makeText(getContext().getApplicationContext(), "已加载数据", Toast.LENGTH_SHORT).show();
                    break;
            }

        }



    };

    private void onLoadComplete() {
        mListView.stopRefresh();
        mListView.stopLoadMore();
        mListView.setRefreshTime(TimeUtils.getCurrentTime());
    }

    private void loadMode(int page_index) {
        SharedPreferences sharedPreferences = getContext().getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String userID = sharedPreferences.getString("userID", "");
        RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/lfl_api/Commonlfl/transfer");
        //需要带入的参数
        params.addBodyParameter("uid", userID);
        params.addBodyParameter("state", 1);
        params.addBodyParameter("page_size", 10);
        params.addBodyParameter("page_index", page_index);

        // params.addBodyParameter("captcha", captcha);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    bCode = jsonObject.optInt("code", 0);
                    String data = jsonObject.optString("data", null);
                    JSONObject jData = new JSONObject(data);
                    JSONArray list = jData.getJSONArray("list");
                    for (int i = 0; i < list.length(); i++) {
                        JSONObject value = list.getJSONObject(i);
                        String sn = value.getString("sn");
                        String lId = value.getString("lId");
                        String nid = value.getString("nid");
                        String merName = value.getString("merName");
                    //    mSnListBean = new MachineBean(lId,nid,sn,merName);
                        SnListBeanListBean.add(mSnListBean);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                if (bCode == 200) {
                    mSnlistAdapter.notifyDataSetChanged();
                }

                if (bCode == 500) {
                    Toast.makeText(getContext().getApplicationContext(), "系统异常", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                onLoadComplete();
            }
        });

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_unactivation, container, false);
        nu_bindingList=view.findViewById(R.id.nu_unactivationList);
        mListView=view.findViewById(R.id.unactivation_list);
        mListView.setRefreshEnable(true);
        mListView.setLoadEnable(true);
        mListView.setAutoLoadEnable(true);
        mListView.setRefreshTime(TimeUtils.getCurrentTime());
        mListView.setListViewPlusListener(this);
        intView();
        return view;
    }
    private void intView() {
        SharedPreferences sharedPreferences = getContext().getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String userID = sharedPreferences.getString("userID", "");
        RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/lfl_api/Commonlfl/transfer");
        //需要带入的参数
        params.addBodyParameter("uid", userID);
        params.addBodyParameter("state", 1);
        params.addBodyParameter("page_size", 10);

        // params.addBodyParameter("captcha", captcha);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    bCode = jsonObject.optInt("code", 0);
                    String data = jsonObject.optString("data", null);
                    JSONObject jData = new JSONObject(data);
                    if (!TextUtils.isEmpty(data)) {
                        JSONObject jsonObjec = new JSONObject(data);
                        count = jsonObjec.optInt("count", 0);
                        page_count = jsonObjec.optInt("page_count", 0);
                        page_index = jsonObjec.optInt("page_index", 0);
                    }
                    JSONArray list = jData.getJSONArray("list");
                    for (int i = 0; i < list.length(); i++) {
                        JSONObject value = list.getJSONObject(i);
                        String sn = value.getString("sn");
                        String lId = value.getString("lId");
                        String nid = value.getString("nid");
                        String merName = value.getString("merName");
                     //   mSnListBean = new MachineBean(lId,nid,sn,merName);
                        SnListBeanListBean.add(mSnListBean);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                if (bCode == 200) {
                    mSnlistAdapter = new MachineAdapter(getContext().getApplicationContext(), SnListBeanListBean);
                    mSnlistAdapter.notifyDataSetChanged();
                    mListView.setAdapter(mSnlistAdapter);
                }

                if (bCode == 500) {
                    Toast.makeText(getContext().getApplicationContext(), "系统异常", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                if (SnListBeanListBean.size() == 0) {
                    nu_bindingList.setVisibility(View.VISIBLE);
                } else {
                    nu_bindingList.setVisibility(View.GONE);
                }
            }
        });
    }
}
